<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="left">
        <img src="../assets/img/shop.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <div class="right">
        <span>欢迎您:{{ userInfo.username }}</span>
        <el-button type="danger" size="mini" @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠 -->
        <div class="toggle-button" @click="isCollapse = !isCollapse">|||</div>
        <!-- 菜单 -->
        <el-menu :default-active="$route.path" unique-opened router :collapse="isCollapse" :collapse-transition="false">
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/' + subItem.path">
              <i class="el-icon-menu"></i>
              {{ subItem.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //用户信息
      userInfo: null,
      //菜单列表
      menuList: [],
      //菜单图标
      iconObj: {
        201: "el-icon-house",
        125: "el-icon-user",
        103: "el-icon-coordinate",
        101: "el-icon-shopping-bag-2",
        102: "el-icon-tickets",
      },
      //是否折叠
      isCollapse: false,
    };
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    this.getMenuList();
  },
  methods: {
    logout() {
      this.$confirm("确定要退出登录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sessionStorage.clear();
          this.$router.push("/login");
          this.$message({
            type: "success",
            message: "退出成功",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消退出",
          });
        });
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status != 200) {
        return this.$message.error(res.meta.msg);
      }
      this.menuList = res.data;
    },
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100vh;
  .el-header {
    background: url("../assets/img/header_bg.gif") repeat-x center;
    height: 50px !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    .left {
      display: flex;
      align-items: center;
      img {
        width: 40px;
        margin-right: 20px;
      }
      span {
        font-size: 20px;
      }
    }
    .right {
      span {
        margin-right: 10px;
      }
    }
  }
}
.el-aside {
  .toggle-button {
    background: #60779d;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    color: #fff;
    letter-spacing: 0.2rem;
    cursor: pointer;
  }
  .el-menu {
    border-right: 0;
    .iconfont {
      margin-right: 10px;
      color: #666;
    }
  }
}
.el-main {
  background: #eaedf1;
}
</style>
